package com.sky.controller.user;

import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userDish")
@RequestMapping("/user/dish")
@Api(tags = "设置菜品接口")
@Slf4j
public class DishController {
    @Autowired
    public DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation(value = "菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);//后续定义
        return Result.success(pageResult);
    }

    /**
     *新增菜品
     *
     * @param dishVO
     * @return
     */
    @ApiOperation(value = "新增菜品")
    @PostMapping
    public Result<String> add(@RequestBody DishVO dishVO){
        log.info("新增菜品信息: {}", dishVO);
        dishService.addDish(dishVO);
        return Result.success();
    }

    /**
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("起售停售菜品")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishCache", allEntries = true)
    public Result<Object> startOrStop(@PathVariable Integer status, long id){
        log.info("起售停售菜品id: {}", id);
        dishService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 修改菜品信息
     *
     * @param dishVO
     * @return
     */
    @ApiOperation("修改菜品信息")
    @PutMapping
    @CacheEvict(cacheNames = "dishCache", allEntries = true)
    public Result<Object> editDishMessage(@RequestBody DishVO dishVO){
        log.info("修改菜品信息: {}", dishVO);
        dishService.edit(dishVO);
        return Result.success();
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @ApiOperation("删除菜品")
    @DeleteMapping
    @CacheEvict(cacheNames = "dishCache", allEntries = true)
    public Result<Object> deleteDish(long[] ids){
        dishService.deleteById(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id回显菜品信息")
    public Result<DishVO> getByID(@PathVariable long id){
        DishVO dishvo = dishService.getById(id);
        return Result.success(dishvo);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    // TODO 设置redis缓存，来提高c端查询效率
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    @Cacheable(cacheNames = "dishCache", key = "#categoryId")
    public Result<List<DishVO>> list(Long categoryId){
        //构造redis中的key，规则：dish_分类id
//        String key = "dish_" + categoryId;
//        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
//        if (list != null && list.size() > 0){
//            return Result.success(list);
//        }
        // 不光要根据id查询，还得是起售的菜品
        // 所以这里需重写一个service通过dish的分类id，加status两个参数查询
        // 这里直接创建一个dish，通过pojo类查询
        Dish dish = Dish.builder()
                .status(1)
                .categoryId(categoryId)
                .build();
        List<DishVO> list = dishService.list(dish);
//        redisTemplate.opsForValue().set(key, list);
        return Result.success(list);
    }
}
